package org.jeecg.modules.custom.service.impl;

import cn.hutool.core.date.DateUtil;
import org.jeecg.modules.custom.entity.MtCustomEquip;
import org.jeecg.modules.custom.mapper.MtCustomEquipMapper;
import org.jeecg.modules.custom.service.IMtCustomEquipService;
import org.jeecg.modules.equipment.entity.MtEquip;
import org.jeecg.modules.equipment.mapper.MtEquipMapper;
import org.jeecg.modules.order.entity.MtRepairEquip;
import org.jeecg.wechat.vo.MtCustomEquipVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 客户设备表
 * @Author: jeecg-boot
 * @Date: 2022-08-19
 * @Version: V1.0
 */
@Service
public class MtCustomEquipServiceImpl extends ServiceImpl<MtCustomEquipMapper, MtCustomEquip> implements IMtCustomEquipService {

    @Autowired
    private MtCustomEquipMapper mtCustomEquipMapper;
    @Autowired
    private MtEquipMapper mtEquipMapper;

    @Override
    public List<MtCustomEquip> selectByMainId(String mainId) {
        return mtCustomEquipMapper.selectByMainId(mainId);
    }

    @Override
    public List<MtCustomEquipVo> getListAndTypeCustom(String customId) {
        return mtCustomEquipMapper.getListAndTypeCustom(customId);
    }

    @Override
    public void repair(String customEquipId) {
        MtCustomEquip customEquip = mtCustomEquipMapper.selectById(customEquipId);
        if (!Objects.isNull(customEquip)) {
            customEquip.setRepairCount(customEquip.getRepairCount() + 1);//维修次数+1
            mtCustomEquipMapper.updateById(customEquip);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void replace(MtRepairEquip mtRepairEquip) {
        // 更新客户旧设备
        MtCustomEquip customEquip = new MtCustomEquip();
        customEquip.setId(mtRepairEquip.getCustomEquipId())//客户设备id
                .setStatus("1");//状态：禁用
        mtCustomEquipMapper.updateById(customEquip);
        // 新增客户新设备
        MtEquip mtEquip = mtEquipMapper.selectById(mtRepairEquip.getNewEquipId());
        MtCustomEquip newCustomEquip = new MtCustomEquip();
        newCustomEquip.setEquipCode(mtRepairEquip.getNewEquipCode())//编号
                .setCustomId(mtRepairEquip.getCustomId())//所属商户
                .setEquipId(mtEquip.getId())//设备id
                .setEuipCount("1")//数量
                .setQualityPeriod(mtEquip.getQualityPeriod())//质保期
                .setInstallDate(DateUtil.date())//安装日期
                .setStationId(mtRepairEquip.getStationId())//负责服务站
                .setProviderId(mtRepairEquip.getProviderId())//负责服务商
                .setImageUri(mtEquip.getImageUri());//设备图片
        newCustomEquip.setPeriodDate(DateUtil.offsetMonth(newCustomEquip.getInstallDate(), newCustomEquip.getQualityPeriod()));//质保到期
        mtCustomEquipMapper.insert(newCustomEquip);
    }
}
